package com.ruoyi.system.ali;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.sign.SignMD5Util;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.UserRate;
import com.ruoyi.system.dto.ApiByPhoneAddDto;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.UserRateService;
import com.ruoyi.system.service.ZfbConfigService;
import com.ruoyi.system.vo.ApiPayResultVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.TreeMap;

/**
 * 当面付
 */
@Component
public class ZzPayService implements ChannelService {
    @Resource
    ISysConfigService configService;

    @Resource
    private ZfbConfigService zfbConfigService;
    @Resource
    private BankService bankService;
    @Resource
    private UserRateService userRateService;

    @Override
    public String getType() {
        return ChannelEnum.ZFB_ZZ.getCode();
    }

    @Resource
    private CashBizService cashService;


    @Resource
    private RedisCache redisCache;

    @Override
    public Object create(AliPayDto aliPayDto, Order order) {
        Bank bank = bankService.getRand(order.getAmount(), getType());
        return create(order, bank);
    }

    @Override
    public Object createByPhone(ApiByPhoneAddDto apiByPhoneAddDto, Order order) {
        Bank bank = bankService.getUserBank(order.getUserId(), order.getAmount(), getType());
        return create(order, bank);
    }


    //转账码
    public Object create(Order order, Bank bank) {
        BigDecimal payAmount = order.getAmount();


        payAmount = BigDecimalUtil.setDecimal(payAmount);

        //冻结收款用户的金额
        CashHistory userCashHis = new CashHistory(bank.getUserId(), payAmount, "reduce", "代收订单", "freeze");
        userCashHis.setOrderNo(order.getOrderNo());
        cashService.updateCash(bank.getUserId(), userCashHis);
        order.setChannelCode(this.getType());
        order.setChannelName(ChannelEnum.getName(this.getType()));
        order.setAccount(bank.getCardNo());
        order.setName(bank.getName());
        order.setType("hand");
        order.setBankName(bank.getBankName());
        order.setUserId(bank.getUserId());
        //  SysUser sysUser = userService.check(bank.getUserId());
        UserRate userRate = userRateService.checkUserRate(bank.getUserId(), this.getType());
        order.setEarning(order.getAmount().multiply(userRate.getRate().divide(new BigDecimal(100))));
        order.setUsername(bank.getUsername());
        ApiPayResultVo apiVo = new ApiPayResultVo();
        apiVo.setAmount(String.valueOf(payAmount));
        apiVo.setOrderNo(order.getOrderNo());
        apiVo.setMerchantId(order.getMerchantId());
        TreeMap<String, String> map = SignMD5Util.objToMap(apiVo);
        //7 数字人民币
        map.put("payType", "8");
        String sign = SignMD5Util.getSign(map, "sign");
        String payUrl = configService.selectConfigByKey("zzpay");
        payUrl = payUrl + "?" + SignMD5Util.getStr(map, sign);
        apiVo.setUrl(payUrl);
        return apiVo;
    }


}
